/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controller;

import java.util.ArrayList;
import java.util.List;
import javax.ejb.Stateful;
import javax.ejb.TransactionAttribute;
import javax.ejb.TransactionAttributeType;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import model.Customer;

/**
 *
 * @author zno
 */
@TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)
@Stateful
public class CustomerController {

    @PersistenceContext(unitName = "ProjectPU")
    private EntityManager em;
    
    /**
     * 
     * @return the list of customers 
     */
    public ArrayList<Customer> listCustomers () {
        Query q = em.createQuery("SELECT c FROM Customer c");
        List<Customer> list = (List<Customer>) q.getResultList();
        ArrayList<Customer> users = new ArrayList<Customer>();
        for (Customer c : list)
            users.add(c);
        
        return users;
    }
    /**
     * 
     * @param usr   User Name
     * @param pwd   Password
     * @return      the status of whether the user is a customer
     */
    public boolean isCustomer (String usr,String pwd) {
        Query q = em.createQuery(
                "SELECT c FROM Customer c "+
                "WHERE c.name LIKE :name AND c.password LIKE :password");
        
        q.setParameter("name", usr);
        q.setParameter("password",pwd);
        
        return !q.getResultList().isEmpty();   
    }
    /**
     * 
     * @param name      User Name
     * @param pwd       Password
     * @param balance   Balance
     * @return          A newly created customer
     */
    public Customer createCustomer(String name,String pwd,int balance) {
        Customer c = new Customer (name,pwd,balance);
        em.persist(c);
        return c;
    }
    /**
     * Deletes an existing customer
     * @param usr       User Name
     * @param pwd       Password
     */
    public void deleteCustomer(String usr,String pwd)
    {
        Query q = em.createQuery("DELETE FROM Customer c WHERE c.name LIKE :name AND c.password LIKE :password");
        q.setParameter("name", usr);
        q.setParameter("password", pwd);
        q.executeUpdate();
    }
    /**
     * 
     * @param usr       User Name
     * @param pwd       Password
     * @return          The status of whether the login was successful or not
     */
    public boolean login (String usr,String pwd) {
        Query q = em.createQuery
                (
                    "UPDATE Customer c SET c.loggedIn = '1' "+
                    
                    "WHERE c.name   LIKE :name      AND "+
                    "c.password     LIKE :password  AND "+
                    "c.banned       LIKE :banned"
                );
        
        
        q.setParameter("name", usr);
        q.setParameter("password", pwd);
        q.setParameter("banned", "0");
        
        return q.executeUpdate() != 0;
    }
    /**
     * 
     * @param usr       User Name
     * @param pwd       Password
     * @return          The status of whether the logout was successful or not
     */
    public boolean logout (String usr,String pwd) 
    {
        Query q = em.createQuery("UPDATE Customer c SET c.loggedIn = '0' WHERE c.name LIKE :name AND c.password LIKE :password");
        
        q.setParameter("name", usr);
        q.setParameter("password", pwd);
     
        return q.executeUpdate() != 0;
    }
    /**
     * Bans a customer from being able to log in
     * @param c Customer
     */
    public void ban (Customer c) {
        Query q = em.createQuery("UPDATE Customer c SET c.banned = '1' "+
                "WHERE c.name LIKE :name AND c.password LIKE :password");
        
        
        q.setParameter("name", c.getName());
        q.setParameter("password", c.getPassword());
        q.executeUpdate();
    }
    /**
     * Unbans a customer from being able to log in
     * @param c Customer
     */
    public void unban (Customer c) {
        Query q = em.createQuery("UPDATE Customer c SET c.banned = '0' "+
                "WHERE c.name LIKE :name AND c.password LIKE :password");
        
        
        q.setParameter("name", c.getName());
        q.setParameter("password", c.getPassword());
        
        q.executeUpdate();
    }
    /**
     * Returns the login credentials for a particular username
     * @param username
     * @return the corresponding Customer object 
     */
    public Customer loginCredentials (String username)
    {
        Query q = em.createQuery("SELECT c FROM Customer c WHERE c.name LIKE :name");
        q.setParameter("name", username);
        Customer c = (q.getSingleResult() != null) ? (Customer)q.getSingleResult() : null;
        
        return c;
    }
}
